<template>
	<view class="container">

		<u--form labelPosition="left" ref="form1" :model='shopInfo' :rules="rules">
			
			<u-form-item labelWidth="80" label="货架名称" prop='name'>
				<u--input placeholder="请输入货架名称" border="bottom" v-model="shopInfo.name"  ></u--input>
			</u-form-item>
			

		</u--form>

		<u-row customStyle="margin: 32rpx 0;">

			<u-col span="10" offset="1">

			<u-button shape='circle' text="新增" size="primary" type="info" @click="add"></u-button>

			</u-col>

		</u-row>

	</view>
</template>

<script>
	var that 
	var ERgoodsShelf = require('../../../service/ERgoodsShelf.js'),mERgoodsShelf
	export default {
		data() {
			return {
				fromScan:false,
				shopId:'',
				shopInfo: {

					name: '',

				},
				rules: {

					name: {
						type: 'string',
						required: true,
						message: '请输入名称',
						trigger: ['blur', 'change']

					}
				},


			}
		},
		onLoad(options) {
			that = this
			if(options){
				that.shopId = options.shopId
				that.fromScan = options.fromScan
			}
			
			mERgoodsShelf = new ERgoodsShelf(this.mNetTool)
		},
		mounted() {
			// let shopList = uni.getStorageSync('shopList')
			// let shopIds = []
			// shopList.forEach(item => {
			// 	shopIds.push(item.id)
			// })
			// uni.$u.toast('正在刷新货架列表....')
			// if(shopList){
			// 	this.mNetTool.getAjaxUrlValue('capi/V2/ERgoodsShelf/erGoodsShelves/list',{
			// 		store_id:shopIds.toString(), 
			// 	},res => {
			// 		console.log( res.rows)
			// 		uni.setStorageSync('shelfList', res.rows )
			// 		 uni.$u.toast('刷新成功')
			// 	})   
			// }
			// that.shopIds = shopIds.toString() 
			
		},
		onReady() {

		},
		methods: {
			// add(){
			// 	uni.$u.throttle(this.toadd, 1000)
			// },
			add() { 
				uni.showLoading({
					title:'请稍等'
				})
				let shelfList = uni.getStorageSync('shelfListConfig')
				let userInfo = uni.getStorageSync('userInfo')
				let viewShopListItem = uni.getStorageSync('viewShopListItem')
				if(that.shopId){
					viewShopListItem.id = that.shopId
				}
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				this.$refs.form1.validate().then(res => {
					let addInfo = {
						store_id:viewShopListItem.id,
						created_by:userInfo.id,
						shelves_name:that.shopInfo.name,
					}
					 
						console.log(addInfo)
					mERgoodsShelf.save(addInfo,res => {
						console.log(res)
						uni.hideLoading()
						if(res.success) {
							addInfo.id = res.id 
							mERgoodsShelf.list({
								store_id:viewShopListItem.id,
							},res => {
								console.log(res)
								if(that.fromScan){
									uni.setStorageSync('shelfList',res.rows)
								}else{
									uni.setStorageSync('shelfListConfig',res.rows)
								}
								uni.$u.toast('添加成功')
								uni.navigateBack({})
							})
							
						}
						})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})

				
			}

		},
		onReady() {


		},
	}
</script>

<style lang="scss">
	.container {
		padding: 32rpx;
	}
</style>
